<?php
require_once ('IImageService.php');
require_once ('BaseService.php');
require_once 'dao/ImageDao.php';
require_once 'util/ServiceUtil.php';
require_once 'util/StringUtil.php';

class ImageService extends BaseService implements IImageService {
	
	public static $NAME="ImageService";
	protected $imageDao;
	
	public function ImageService() {
		$this->initializeDB();
		$this->imageDao = new ImageDao();
	}
	
	
	public function create(ImageDao $object) 
	{
		if($object)
		{
			try
			{
				$this->sql = "INSERT INTO image(description,title,url) VALUES('$object->description','$object->title','$object->url')";
				$this->query = mysql_query($this->sql,$this->myDb);
				return $this->checkQueryResult(mysql_affected_rows($this->myDb));
			}catch(Exception $exc)
			{
				return $exc;
			}
		}
		return NULL;
	}
	
	public function delete($id) 
	{
		if($id)
		{
			try
			{
				$this->sql = "DELETE FROM image WHERE id = '$id'";
				$this->query = mysql_query($this->sql,$this->myDb);
				
				return $this->checkQueryResult(mysql_affected_rows($this->myDb));
				
			}catch(Exception $exc)
			{
				return $exc;
			}
		}
		return NULL;
	}
	
	public function read($id) 
	{
		if($id==NULL||$id==-1)
		{
			$this->sql = "SELECT * FROM image";
		}else
		{
			$this->sql = "SELECT * FROM image WHERE id='$id'";
		}
		try
		{
			$this->query = mysql_query($this->sql,$this->myDb);
			$this->numRows = mysql_num_rows($this->query);
			
			for($i=0;$i<$this->numRows;$i++)
 			{
 				$row = mysql_fetch_object($this->query);
 				
 				$this->imageDao = new ImageDao();
 				
 				$this->imageDao->id = $row->id;
				$this->imageDao->description = $row->description;
				$this->imageDao->title = $row->title;
				$this->imageDao->url = $row->url;
				$this->imageDao->time = $row->time;
					
				$this->result[] = $this->imageDao;
 			}
			return $this->result;
		}catch(Exception $exc)
		{
			return $exc;
		}
		return NULL;
	}
	
	public function update(ImageDao $object) 
	{
		if($object)
		{
			try
			{
				$this->sql = "UPDATE TABLE image SET description='$object->description',
							title='$object->title',url='$object->url'";
				$this->query = mysql_query($this->sql,$this->myDb);
				return $this->checkQueryResult(mysql_affected_rows($this->myDb));
			}catch(Exception $exc)
			{
				return $exc;
			}
		}
		return NULL;
	}
}

?>